package com.kongge.formlikedemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.kongge.formlike.Form;
import com.kongge.formlike.OnFormCommitListener;
import com.kongge.formlike.service.check.ICheckItem;
import com.kongge.formlike.service.rule.IRule;
import com.kongge.formlikedemo.check.DIYCheckView;
import com.kongge.formlike.service.rule.RuleTextMatch;
import com.kongge.formlikedemo.view.DIYView;

import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private static final String STR_METHOD = "method";
    private static final String STR_ACTION = "action";

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        setContentView(view);

        DIYView diyView = findViewById(R.id.diy_view);
        diyView.setText("abxcSDDFsdcAd");

        Form.setCheckView(DIYCheckView.class);

        String jsonStr = FileUtil.parseAssetsFile(this, "config/testConfig01.json");
        Form form = new Form();
        form.parseView(view, jsonStr);
        form.setFormCommitListener(new OnFormCommitListener() {
            @Override
            public void onFormCommitChecked(Form form, Map<String, Object> configMap, Map<String, String> paramMap) {
                Toast.makeText(MainActivity.this, "method=" + configMap.get(STR_METHOD) + "\nurl=" + configMap.get(STR_ACTION) + "\n" + "paramMap=" + paramMap.toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFormCommitError(Form form, ICheckItem checkItem, IRule errorRule) {
                Toast.makeText(MainActivity.this, errorRule.getErrorMsg(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
